package com.hmy.finance.service.core.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmy.finance.common.enums.BusinessBillTypeEnum;
import com.hmy.finance.common.enums.PaymentStatusEnum;
import com.hmy.finance.dal.mapper.ProfitLossRecordMapper;
import com.hmy.finance.dal.po.ProfitLossRecordPo;
import com.hmy.finance.service.core.IProfitLossRecordService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * @author songkf
 * @description 针对表【profit_loss_record(损益业务记录表)】的数据库操作Service实现
 * @createDate 2024-02-21 18:55:01
 */
@Service
public class ProfitLossRecordServiceImpl extends ServiceImpl<ProfitLossRecordMapper, ProfitLossRecordPo>
        implements IProfitLossRecordService {

    @Override
    public List<ProfitLossRecordPo> selectReceivableAdjustListByContractIdAndOffsetThroughDate(Long contractId, LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<ProfitLossRecordPo> lambdaQueryWrapper = Wrappers.lambdaQuery(ProfitLossRecordPo.class)
                .in(ProfitLossRecordPo::getPaymentStatus, PaymentStatusEnum.PARTIAL_PAYMENT, PaymentStatusEnum.UNPAID)
                .eq(ProfitLossRecordPo::getBusinessBillType, BusinessBillTypeEnum.RECEIVABLE_ADJUSTMENT)
                .eq(ProfitLossRecordPo::getContractId, contractId);
        //       .ge(Objects.nonNull(startDate), ProfitLossRecordPo::getFeeIncurredTime, startDate.atStartOfDay())
        //                .le(Objects.nonNull(endDate), ProfitLossRecordPo::getFeeIncurredTime, endDate.atTime(LocalTime.MAX));
        if (startDate != null) {
            lambdaQueryWrapper.ge(ProfitLossRecordPo::getFeeIncurredTime, startDate.atStartOfDay());
        }
        if (endDate != null) {
            lambdaQueryWrapper.le(ProfitLossRecordPo::getFeeIncurredTime, endDate.atTime(LocalTime.MAX));
        }

        return this.baseMapper.selectList(lambdaQueryWrapper);
    }
}




